package com.cxrj.zhsq.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class LoginInterceptor extends HandlerInterceptorAdapter {
	 
	    @Override
	    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
	    	boolean flag = false;
//	    	System.err.println("=================>拦截");
        	String userId=String.valueOf(request.getSession().getAttribute("userId"));
        	String url = request.getRequestURL().toString();
			String uri = request.getRequestURI();
			String contextPath = request.getContextPath();
			String path = uri.replaceFirst(contextPath,"");
			/*System.err.println("url="+url);
			System.err.println("uri="+uri);
			System.err.println("contextPath="+contextPath);
			System.err.println("path="+path);*/
//        	System.err.println(">>>>>>>>>>>>>>>>>>userId:"+userId);
            if (userId!=null&&(!userId.equals(""))&&(!userId.equals("null"))){
            	flag = true;
            }
            else if (path.equals("/apiAcquisition") || path.startsWith("/spider")){
//				System.err.println("进来了");
				flag = true;
			}
            else{
            	flag = false;
    	        response.sendRedirect("/zhsq_xg/jump_login");
            }
            
	        return flag;
	    }
	    @Override
	    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
	        super.postHandle(request, response, handler, modelAndView);
	    }
}

